<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>新增商店</title>
    <link rel="stylesheet" href="layui/css/layui.css">
    <link rel="stylesheet" href="static/css/style.css">
    <link rel="stylesheet" href="layui/css/layuibtn.css">
</head>
<style>
    .roomType {
        display: block !important;
        width: 100%;
        border-color: #D2D2D2 !important;
        height: 38px;
        line-height: 1.3;
        border-width: 1px;
        border-style: solid;
        background-color: #fff;
        border-radius: 2px;
        padding-left: 10px;
    }

    .fileimg {
        margin-left: 0;
        margin-top: 0;
        width: 100px;
        z-index: 1;
    }

    #fileimg {
        width: 100px;
        height: 38px !important;
        overflow: hidden;
        height: 100px;
        opacity: 0;
        position: absolute;
        top: 0;
        left: 0;
        z-index: 10;
        margin: 0;
        padding: 0;
    }
</style>

<body>
<div class="container">
    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
        <legend>新增商店</legend>
    </fieldset>

    <form class="layui-form" action="">
        <div class="layui-form-item">
            <label class="layui-form-label">用户名字</label>
            <div class="layui-input-block">
                <input type="text" name="username" required autocomplete="off" placeholder="请输入"
                       class="layui-input username" id="username">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">商店名称</label>
            <div class="layui-input-block">
                <input type="text" name="name" required autocomplete="off" placeholder="请输入"
                       class="layui-input name" id="name">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">手机号</label>
            <div class="layui-input-block">
                <input type="text" name="phone" required autocomplete="off" placeholder="请输入手机号"
                       class="layui-input phone" id="phone">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">密码</label>
            <div class="layui-input-block">
                <input type="text" name="password" required autocomplete="off" placeholder="请输入密码"
                       class="layui-input password" id="password">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">经营范围</label>
            <div class="layui-input-block">
                <input type="text" name="range" required autocomplete="off" placeholder="请输入经营范围"
                       class="layui-input range" id="range">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">公告</label>
            <div class="layui-input-block">
                <input type="text" name="notice" autocomplete="off" placeholder="请输入公告"
                       class="layui-input notice" id="notice">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">图片</label>
            <div class="layui-input-block ">
                <img style="width: 150px;height: 150px;" class="logo" id="demo1" src=""/>
            </div>
            <br/>
            <div class="layui-input-block">
                <div class="layui-btn fileimg" id="test1">点击修改</div>
                <span class="filename"></span>
                <input type="file" class="layui-btn fileimg" id="fileimg">
                <h2 disabled id="test"></h2>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">商家地址</label>
            <div class="layui-input-block">
                <input type="text" name="address" required autocomplete="off" placeholder="请输入地址"
                       class="layui-input address" id="address">
            </div>
        </div>

        <!--        <div class="layui-form-item">-->
        <!--            <label class="layui-form-label">抽成</label>-->
        <!--            <div class="layui-input-block">-->
        <!--                <input type="text" name="proportion" lay-verify="proportion" autocomplete="off" placeholder="请输入抽成"-->
        <!--                       class="layui-input proportion">-->
        <!--            </div>-->
        <!--        </div>-->
        <div class="layui-form-item">
            <label class="layui-form-label">分类</label>
            <div class="layui-input-block">
                <from class="layui-form">
                    <select name="typeid" lay-verify="typeid" id="typeid">
                        <option value="0">请选择一个分类</option>
                    </select>
                </from>
            </div>
        </div>
        <div class="layui-form-item">
            <button type="button" class="layui-btn" onclick="settijiao()">提交</button>
            <button type="button" class="layui-btn" id="quxiao">取消</button>
        </div>
    </form>
</div>

<script type="text/javascript" src="js/jquery.min.js"></script>
<script src="js/common.js"></script>
<script src="layui/layui.js"></script>
<script src="layer-v3.1.1/layer/mobile/layer.js"></script>
<script>
    var $
    var form

    //$(function() {
    layui.use(['element', 'layer', 'form'], function () {
        $ = layui.jquery;
        layer = layui.layer;
        form = layui.form; //只有执行了这一步，部分表单元素才会自动修饰成功
    });
    var dinId = getQueryString("roomId");
    var xx = '';

    $(".fileimg").on('change', function () {
        var fileimg = document.getElementById('fileimg').files[0].name;
        var file = document.getElementById('fileimg').files[0];

        $(".filename").html("图片为：" + fileimg)
        var fd = new FormData();
        fd.append('zurl', fileimg)
        console.log(document.getElementById('fileimg').files[0])
        $("#demo1").attr("src", URL.createObjectURL(file));

        var fileimg = document.getElementById('fileimg').files[0];
        var files = new FormData();
        files.append('files', fileimg)
        $.ajax({
            type: "post",
            data: files,
            cache: false,
            processData: false,
            contentType: false,
            url: baseurl + "/subordinate/addimg",
            success: function (data) {
                $("#test").html(data.data)
            }
        });

    })

    $.ajax({
        type: "post",
        url: baseurl + "/large/query",
        success: function (data) {

            for (i = 0; i < data.data.length; i++) {
                xx = xx + `
					        <option value="` + data.data[i].largeId + `">` + data.data[i].largeName + `</option>
                        `;
            }
            $("#typeid").html(`
				<option value="0">请选择一个分类</option>` + xx
            );

        }
    });
    //
    // $.ajax({
    //     type: "post",
    //     url: baseurl + "/user/query",
    //     success: function (data) {
    //
    //         for (i = 0; i < data.data.length; i++) {
    //             xx = xx + `
    // 				        <option value="` + data.data[i].largeId + `">` + data.data[i].largeName + `</option>
    //                     `;
    //         }
    //         $("#typeid").html(`
    // 			<option value="0">请选择一个分类</option>` + xx
    //         );
    //
    //         form.render();
    //     }
    // });
    $("#quxiao").click(function () {
        window.parent.location.reload();
    })

    function settijiao() {
        if ($("#test").html() == "" || $("#user").val() == "" || $("#address").val() == "" || $("#phone").val() == "" || $("#typeid").val() == "0" || $("#typeid").val() == 0 || $("#typeid").val() == "" || $("#range").val() == "") {
            layer.msg('请填写完全，公告可不填', {icon: 4});
            return false
        }
        $.ajax({
            type: "post",
            url: baseurl + "/subordinate/add",
            data: {
                username: $("#user").val(),
                name: $("#name").val(),
                address: $("#address").val(),
                phone: $("#phone").val(),
                password: $("#password").val(),
                region: $("#typeid").val(),
                range: $("#range").val(),
                notice: $("#notice").val(),
                img: $("#test").html()
            },
            success: function (data) {
                console.log(data)
                if (data.meta.code == 200) {
                    layer.alert('新增成功', function () {
                        window.parent.location.reload();
                    });
                } else {
                    layer.alert("新增失败" + data.meta.msg);
                }
            }
        })

    }
</script>
</body>

</html>